package test.config;

import com.ulisesbocchio.jasyptspringboot.EncryptablePropertyDetector;
import com.ulisesbocchio.jasyptspringboot.EncryptablePropertyResolver;
import com.ulisesbocchio.jasyptspringboot.detector.DefaultPropertyDetector;
import com.ulisesbocchio.jasyptspringboot.exception.DecryptionException;
import org.jasypt.exceptions.EncryptionOperationNotPossibleException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import test.utils.RsaUtil;

import javax.annotation.Resource;
import java.util.Optional;

/**
 * Created with IntelliJ IDEA.
 *
 * @ project name : example
 * @ Author: XuLeHuang
 * @ Date: 2022/8/15 14:06
 * @ Description:
 */
public class MyEncryptPropertyResolver implements EncryptablePropertyResolver {
    @Autowired
    private DefaultPropertyDetector defaultPropertyDetector;

    @Override
    public String resolvePropertyValue(String encodePassword) {
        System.out.println(defaultPropertyDetector);
        return Optional.ofNullable(encodePassword)
                .filter(defaultPropertyDetector::isEncrypted)		// 如果经过属性探测器确认的，才继续
                .map(resolvedValue -> {
                    try {
                        String unwrappedProperty = defaultPropertyDetector.unwrapEncryptedValue(resolvedValue.trim());	// 过滤加密规则后的字符串
                        return RsaUtil.decrypt(unwrappedProperty, "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCefLM+MwShRL/dKebK1UpeGGhZeV2Us+7cz6mSHnptRkGF5wnT6JefXz/HT+XnLK1TYkGbG3o4puQ54E/VzFcc+QI99X1Sz9LC0m8lDrEyUI5OsF09f6RpZcoH2kvJWuYI1huhRZ4hDM2/3e/cudv5QpvHtZEh48YIChD4rayGRQvrJ0KLSOF8xCfHfTFFvp0KrYUXTX+FZej6n7YeveQp2VWrHA2gGQqHTKiu499GULzY9mL4PSaLbjqDc9EgRB7Z9hk0lWHo2jeeigQcfPgFVKoGnV/iJm3NuIHyAlGwZoINUw8tuufTTOHm7aUa+fcf04u+F0Tpr5s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8ISWuiTBxJ+1GMxNs8jaD9arAgX5G0vjFScpXgxxiflBdrYdcMCKEq+vreO9RejvbLajMLPqujjqECg+SVZuxn9XuFgTeTKnmIxVwfeMRW0kmWh41aiMEbHRPqzp1SAjL0345SQ+Q6Il2KqHBdZborkmfO68Ghnk");
                        // 解密
                    } catch (EncryptionOperationNotPossibleException e) {
                        throw new DecryptionException("Unable to decrypt: " + encodePassword + ". Decryption of Properties failed,  make sure encryption/decryption " +
                                "passwords match", e);
                    }
                })
                .orElse(encodePassword);

        /*if (!StringUtils.isEmpty(encodePassword) && encodePassword.startsWith("ENC(")) {
            String newEncodePassword = encodePassword.replace("ENC(", "");
            newEncodePassword = newEncodePassword.replace(")" , "");
            String decodePassword = null;
            try {
                decodePassword = RsaUtil.decrypt(newEncodePassword, "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCefLM+MwShRL/dKebK1UpeGGhZeV2Us+7cz6mSHnptRkGF5wnT6JefXz/HT+XnLK1TYkGbG3o4puQ54E/VzFcc+QI99X1Sz9LC0m8lDrEyUI5OsF09f6RpZcoH2kvJWuYI1huhRZ4hDM2/3e/cudv5QpvHtZEh48YIChD4rayGRQvrJ0KLSOF8xCfHfTFFvp0KrYUXTX+FZej6n7YeveQp2VWrHA2gGQqHTKiu499GULzY9mL4PSaLbjqDc9EgRB7Z9hk0lWHo2jeeigQcfPgFVKoGnV/iJm3NuIHyAlGwZoINUw8tuufTTOHm7aUa+fcf04u+F0Tpr5s55W4Iog+RAgMBAAECggEBAJfmUvFReqWKvA2gZHiRVxa6dAEVppCoqE5wpkjK93IWfSR2nN/CtNfmNb21IVehZXpe2RxtiIYVxvaFvF7g27mEHBcAXB5r28goYqh03Xao6MwYkzUnSpBOJAXEvxB9E67yb0bx764jlGvJrmSyqlfL9HzD1BdUNPBtnXiLFKkWsEGPANMnaojV8z4Ak+YqZI6utYuit+/g04Kdb81KaFrpyWcBtDF2SlgntCLhZo+L01E1hfK/FWNipcmW6Gx2HIMrifGabqi0xB5Ub8TiunAb/wZunQ3mW65kB5LUBI7vPBl/GMMman8/yJlPQz76pfS2Y//xWEWNiceC2lnYzjECgYEAy/WqUNhRJ8KfDLzv1Z33A4XtH7Pa9MDcw6jdS3hy2vFsfWs7+1ZN/CbYjI4DJXIVOJqkxCxVlFqYX9LuQmIanv5ds0ciCNv0Rj2rXjI83/R4CPO3unQT8f3WIW/P6CEJFWmmwLt4+/AomJBQjlzUvhqEMBnFPEvddwpFIF1Ks60CgYEAxuzWrtes74USFaQpcPe6tbBQfuiNEExDEH7Rwm5E6noYNbIKTlM43ZT95W5/07N6vu6JJknS6ELfsB1uTFNUoJ/XxvVmiHtFh0UhBdMXQVotA/yTlyOMYdTIyVh14dSzIC+75glvyyt4V5Dat7cfo7KktcvwFpV9uv+7OjBv5/UCgYB4ftR2GrYnZUWNwvl2HHLYdADZH+qVenNybIsAkVJZoxPTLM0s+ZntBA4fS670Kmf7HvGErCSLFtK6meUE9ux/TguRZM7oqPmxnbFmXwuzy7w2nbXHw9gcuTgOXP5ZR1sUeWx4pMkfHoz9RRUyZgmRnhzGRhNOQgu+ziR0sGj3qQKBgQC0vNFd8JWu1hAVW59PhbW9iKHKfM5WRYQVg7Rf3MJ+7En813PnsFOVd96ry5cWqRbYTXCNOjwHTWNh54uki33zpudtXOA5Fw+YFOYpHZ8egKkv2xfaFbFh3exEjDwm4JOTpesp2d8aELf5j6I5k4eKuzBOASp1yoN/GQVqnEHA1QKBgEYu7g2eeLnC8ISWuiTBxJ+1GMxNs8jaD9arAgX5G0vjFScpXgxxiflBdrYdcMCKEq+vreO9RejvbLajMLPqujjqECg+SVZuxn9XuFgTeTKnmIxVwfeMRW0kmWh41aiMEbHRPqzp1SAjL0345SQ+Q6Il2KqHBdZborkmfO68Ghnk");
            } catch (Exception e) {
                e.printStackTrace();
            }
            return decodePassword;
        }
        return encodePassword;*/
    }
}
